LAMP之Yum方式安装

1. 准备工作

1.1. 安装Linux系统

安装Linux系统,这里以CentOS7版本作为演示。并配置好网络环境,确保机器能够上网,可以获取到YUM源。这样才能够以Yum的方式来安装LAMP环境。

1.2. 配置防火墙

配置防火墙,开启80、3306端口。因为CentOS7默认使用firewall作为防火墙,这里改为iptables防火墙。步骤如下:

  • 停止firewall服务:systemctl stop firewalld.service
  • 禁止firewall开机启动:systemctl disable firewalld.service
  • 安装iptables:yum install -y iptables-services
  • 编辑iptables防火墙配置文件vim /etc/sysconfig/iptables,加入下面两行代码,注意位置一定要对应:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    # Firewall configuration written by system-config-firewall
    # Manual customization of this file is not recommended.
    *filter
    :INPUT ACCEPT [0:0]
    :FORWARD ACCEPT [0:0]
    :OUTPUT ACCEPT [0:0]
    -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT
    -A INPUT -p icmp -j ACCEPT
    -A INPUT -i lo -j ACCEPT
    -A INPUT -m state --state NEW -m tcp -p tcp --dport 22 -j ACCEPT
    -A INPUT -m state --state NEW -m tcp -p tcp --dport 80 -j ACCEPT #<==这是第一行
    -A INPUT -m state --state NEW -m tcp -p tcp --dport 3306 -j ACCEPT #<==这是第二行
    -A INPUT -j REJECT --reject-with icmp-host-prohibited
    -A FORWARD -j REJECT --reject-with icmp-host-prohibited
    COMMIT
  • 保存退出后重启防火墙使配置生效:systemctl restart iptables.service

  • 设置防火墙开机启动:systemctl enable iptables.service

1.3. 关闭SELinux

  • 修改配置文件:vim /etc/selinux/config,改成如下:

    1
    2
    3
    #SELINUX=enforcing
    #SELINUXTYPE=targeted
    SELINUX=disabled #<==其实仅仅是改了这里
  • 保存退出使配置立即生效:setenforce 0

2. 安装LAMP环境

2.1. 安装Apache

安装Apache:yum install -y httpd

安装后可能会用到的命令:

  • 启动Apache:systemctl start httpd.service
  • 重启Apache:systemctl restart httpd.service
  • 停止Apache:systemctl stop httpd.service
  • 设置Apache开机启动:systemctl enable httpd.service

重启Apache服务并用浏览器访问该IP地址查看是否安装成功。

2.2. 安装MySQL

由于YUM源上没有mysql-server,所以必须去官网下载MySQL的YUM源,这里我们用wget命令直接获取安装。如下:

  • 获取源:wget http://dev.mysql.com/get/mysql-community-release-el7-5.noarch.rpm
  • 安装源:rpm -ivh mysql-community-release-el7-5.noarch.rpm
  • 安装MySQL:yum install -y mysql-community-server

安装完成后重启MySQL:systemctl restart mysqld.service

安装MySQL后:

  • 初始安装,root用户没有密码:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    # mysql -u root
    Welcome to the MySQL monitor. Commands end with ; or \g.
    Your MySQL connection id is 2
    Server version: 5.6.39-log MySQL Community Server (GPL)
    Copyright (c) 2000, 2018, Oracle and/or its affiliates. All rights reserved.
    Oracle is a registered trademark of Oracle Corporation and/or its
    affiliates. Other names may be trademarks of their respective
    owners.
    Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
    mysql>
  • 设置MySQL密码为123456

    1
    2
    mysql> set password for 'root'@'localhost' =password('123456');
    Query OK, 0 rows affected (0.00 sec)
  • 远程连接设置,所有以root账号连接的远程用户,设其密码为123456

    1
    2
    mysql> grant all privileges on *.* to root@'%' identified by '123456';
    Query OK, 0 rows affected (0.01 sec)
  • 更新权限

    1
    2
    mysql> flush privileges;
    Query OK, 0 rows affected (0.00 sec)

2.3. 安装PHP

  • 安装PHP主程序:yum install -y php
  • 安装PHP组件,使PHP支持MySQL:

    1
    2
    yum install -y php-mysql php-gd libjpeg* php-ldap php-odbc php-pear \
    php-xml php-xmlrpc php-mbstring php-bcmath php-mhash
  • 重启对应服务:

    • systemctl restart mysqld.service
    • systemctl restart httpd.service
  • 查看安装环境版本:
    • cd /var/www/html,新建index.php文件,输入:
      1
      2
      <?php
      phpinfo();

通过浏览器访问,可查看PHP版本信息。


到此,LAMP环境搭建完毕。


0%